<?php
namespace Common\Model;
use Common\Model\BaseModel;
/**
 * 权限规则model
 */
class ProductModel extends BaseModel{


	/**
	 * 初始化
	 */
	public function __construct(){

	}

	/**
	 * 首页
	 */
	public function index($total_page = 10){

			//搜索类型
			$typeid = I("get.typeid",0,'int');

			//关键词
			$keywords = I("get.keywords");

			//页码
			$start_page = I("get.p",0,'int');

			//获取商品类目
			$GetClass  = $this->GetClass();

			//获取商品列表
			$where = array(
				"user_id"=>session('user.id')
			);

			if(!empty($keywords)){

				if(empty($typeid)){
					$where['id'] = array("like","%$keywords%");
				}else if($typeid == 1){

					//获取层级关联分类ID
					$ClassId = $this->ClassId($keywords);

					$where['goods_class_id'] = array("in",$ClassId);

				}else{
					$where['goods_name'] = array("like","%$keywords%");
				}

			}

			$items = M("goods")->where($where)->page($start_page,$total_page)->order("id desc")->select();

			$list = array();

			foreach($items as $k=>$item){

				$list[$k] = $item;

				if(!empty($item['goods_class_id'])){
					$list[$k]['goods_class_name'] = $this->ClassName($item['goods_class_id']);
				}else{
					$list[$k]['goods_class_name'] = "";
				}

			}

			$count = M("goods")->where($where)->count();

			$pages = $this->pageClass($count,$total_page);

			$data = array(
				"getclass"=>$GetClass,
				"items"=>$list,
				"pages"=>$pages,
			);

			return $data;

	}

	/**
	 * 无限级获取 - 获取商品类目
	 */
	public function GetClass($pid = 0){

		$goods_list = M("goods_class")->select();

		$rows = array();

		foreach($goods_list as $k=>$list){

			if($list['pid'] == $pid){
				$list['_data'] = $this->GetClass($list['id']);
				$rows[] = $list;
			}

		}

		return $rows;

	}

	/**
	 * 获取层级分类名称
	 *
	 */
	public function ClassName($id,&$class_name = array()){

		$goods_class = M("goods_class")->where("id=".$id)->find();

		if(!empty($goods_class)){

			$class_name[] = $goods_class['name'];

			$this->ClassName($goods_class['pid'],$class_name);

		}

		return implode("-",array_reverse($class_name));
	}

	/**
	 * 获取层级ID
	 */
	public function ClassId($id,&$class_Id = array()){

		$goods_class = M("goods_class")->where("id=".$id)->find();

		if(!empty($goods_class)){

			//条件为不限
			if($goods_class['typeid'] == 1){

				$class_Id = $this->GetLevel($goods_class['pid']);

			}else{

				$class_Id[] = $goods_class['id'];

				$this->ClassId($goods_class['pid'],$class_Id);
			}

		}

		return $class_Id;
	}

	/**
	 * 获取所有关联ID
	 */
	public function GetLevel($id,&$class_Id = array()){

		$goods_list = M("goods_class")->select();

		foreach($goods_list as $k=>$list){

			if($list['pid'] == $id){
				$class_Id[] = $list['id'];
				$this->GetLevel($list['id'],$class_Id);
			}
		}

		return $class_Id;


	}


	/**
	 * 添加商品
	 */
	public function addProduct(){

		$post = I("post.");

		$goods_name = trim($post['goods_name']);

		$where['user_id'] = array("eq",session('user.id'));
		$where['goods_name']= array("eq",$goods_name);

		$info = M("goods")->where($where)->find();

		if(!empty($info)){

			$rows = array(
				"code"=>400,
				"msg"=>"商品新的名称不能和其他商品重复"
			);

		}else{

			if(!empty($post['goods_name']) && !empty($post['goods_class_id'])){

				$data = array(
					"goods_name"=>$post['goods_name'],
					"goods_class_id"=>$post['goods_class_id'],
					"goods_edit_time"=>$post['goods_edit_time'],
					"user_id"=>session('user.id')
				);

				$id = M("goods")->data($data)->add();

				$url = U('Admin/Page/index',array('keywords'=>$id,'key_id'=>3,'page_state'=>3,'typeid'=>0));

				$rows = array(
					"code"=>200,
					"msg"=>$url,
					"goods_id"=>$id
				);

			}else{

				$rows = array(
					"code"=>400,
					"msg"=>"系统繁忙，请稍后重试!"
				);
			}
		}

		return $rows;

	}

	/**
	 * 编辑商品
	 */
	public function editProduct(){

		$post = I("post.");

		$goods_name = trim($post['goods_name']);

		$where['user_id'] = array("eq",session('user.id'));
		$where['goods_name']= array("eq",$goods_name);
		$where['id']= array("neq",$post['goods_id']);

		$info = M("goods")->where($where)->find();

		if(!empty($info)){

			$rows = array(
				"code"=>400,
				"msg"=>"商品新的名称不能和其他商品重复"
			);

		}else{

			if(!empty($post['goods_name']) && !empty($post['goods_class_id'])){

				$data = array(
					"goods_name"=>$post['goods_name'],
					"goods_class_id"=>$post['goods_class_id'],
					"goods_edit_time"=>$post['goods_edit_time'],
				);

				M("goods")->where('id='.$post['goods_id'])->setField($data);

				$rows = array(
					"code"=>200,
					"msg"=>"OK"
				);

			}else{


				$rows = array(
					"code"=>400,
					"msg"=>"系统繁忙，请稍后重试!"
				);

			}


		}

		return $rows;
	}


}
